<template>
  <div class="login container">
    <Header><span>注册</span></Header>
    <section>
      <div class="login-tel">
        <input
          type="text"
          placeholder="请输入手机号"
          pattern="[0-9*]"
          v-model="userTel"
        />
      </div>
      <div class="login-code">
        <input
          type="text"
          placeholder="请输入短信验证码"
          pattern="[0-9*]"
          v-model="userCode"
        />
        <button @click="sendCode" :disabled="disabled">{{ codeMsg }}</button>
      </div>
      <div class="login-pwd">
        <input type="text" placeholder="请设置密码" v-model="userPwd" />
      </div>
      <div class="login-btn" @click="register">注 册</div>
      <div class="agree">
        注册即视为同意<a href="javascript:void(0);">《服务协议》</a>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>
  
  <script>
import Tabbar from "@/components/common/Tabbar.vue";
import Header from "@/components/common/Header.vue";
import { Toast } from "mint-ui";
//引入封装后的axiox
import http from "@/common/api/request";
import { mapMutations } from "vuex";
export default {
  components: { Tabbar, Header },
  name: "Register",
  data() {
    return {
      disabled: false,
      userTel: "",
      userCode: "",
      userPwd: "",
      rules: {
        //手机号验证
        userTel: {
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空,且为11位数字",
        },
        //密码验证
        userPwd: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空，要求有6~12位",
        },
      },
      codeTime: 6,
      codeMsg: "获取短信验证码",
      code: "",
    };
  },
  methods: {
    ...mapMutations(["USER_LOGIN"]),
    //发送手机验证码
    sendCode() {
      //验证
      if (!this.validata("userTel")) return;
      http
        .$axios({
          url: "/api/code",
          method: "Post",
          data: {
            phone: this.userTel,
          },
        })
        .then((res) => {
          if (res.success) {
            this.code = res.data;
            console.log(this.code);
          }
        });

      //禁用按钮
      this.disabled = true;
      //倒计时
      let timer = setInterval(() => {
        --this.codeTime;
        this.codeMsg = `${this.codeTime}S后可重新发送`;
      }, 1000);
      //判断什么时候停止定时器
      setTimeout(() => {
        clearInterval(timer);
        this.codeNum = 6;
        this.disabled = false;
        this.codeMsg = "获取短信验证码";
      }, 6000);
    },
    //验证信息提示
    validata(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        Toast(this.rules[key].msg);
        bool = false;
      }
      return bool;
    },
    register() {
      //验证手机号格式
      if (!this.validata("userTel")) return;
      //验证码
      if (this.code == "") {
        Toast("验证码不能为空");
        return;
      }
      //验证密码
      if (!this.validata("userPwd")) return;

      //判断验证码正确，发送请求保存数据，注册
      if (this.code == this.userCode) {
        http
          .$axios({
            url: "/api/register",
            method: "Post",
            data: {
              phone: this.userTel,
              pwd: this.userPwd,
            },
          })
          .then((res) => {
            if (res.success) {
              console.log(res);
              Toast(res.msg);
              //登录成功，跳转页面,储存用户信息
              this.USER_LOGIN(res.data);
              this.$router.push("/my");
            } else {
              Toast(res.msg);
              this.userTel = "";
              this.userCode = "";
              this.userPwd = "";
            }
          });
      } else {
        Toast("验证码不正确");
      }
    },
  },
};
</script>
  
  <style scoped lang="scss">
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f5f5f5;
  font-size: 0.32rem;
  div {
    margin: 0.266666rem 0;
    width: 8.933333rem;
    height: 1rem;
  }
  input {
    box-sizing: border-box;
    padding: 0 0.3rem;
    line-height: 1rem;
    background-color: #fff;
    border-radius: 8px;
  }
  .login-tel {
    margin-top: 0.8rem;
    input {
      width: 8.933333rem;
    }
  }

  .login-pwd {
    input {
      width: 8.933333rem;
    }
  }
  .login-code {
    display: flex;
    justify-content: space-between;
    input {
      flex: 1;
    }
    button {
      margin-left: 15px;
      padding: 0 0.533333rem;
      line-height: 1rem;
      color: #fff;
      background-color: #b0352f;
      border: 0;
      border-radius: 4px;
    }
  }
  .login-btn {
    line-height: 1rem;
    background-color: #b0352f;
    border-radius: 6px;
    text-align: center;
    color: #fff;
    font-size: 0.48rem;
  }

  .agree {
    font-size: 0.32rem;
    a {
      color: red;
    }
  }
}
</style>